package nz;

import javax.swing.*;
import java.awt.*;
import java.sql.*;

public class UserProfileUI extends JPanel {
    private Connection connection;
    private JLabel lblUserNo;
    private JTextField txtUserNo;
    private JLabel lblName;
    private JTextField txtName;
    private JLabel lblGender;
    private JComboBox<String> cmbGender;
    private JLabel lblPassword;
    private JPasswordField txtPassword;
    private JLabel lblPhone;
    private JTextField txtPhone;
    private JLabel lblEmail;
    private JTextField txtEmail;
    private JButton btnSubmit;

    public UserProfileUI(String Uno) {
        try {
            connection = ConnectDB.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        setLayout(new BorderLayout(20, 20));
        setBackground(new Color(240, 240, 240));  // 设置背景为浅灰色

        // 创建主面板
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridBagLayout());
        formPanel.setBackground(new Color(240, 240, 240));  // 背景色

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);  // 添加间距
        gbc.anchor = GridBagConstraints.WEST;  // 对齐方式

        // 用户编号
        lblUserNo = new JLabel("用户编号:");
        lblUserNo.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        txtUserNo = new JTextField(20);
        txtUserNo.setText(Uno);
        txtUserNo.setEditable(false);
        txtUserNo.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 用户姓名
        lblName = new JLabel("姓名:");
        lblName.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        txtName = new JTextField(20);
        txtName.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 性别
        lblGender = new JLabel("性别:");
        lblGender.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        cmbGender = new JComboBox<>(new String[]{"男", "女"});
        cmbGender.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 密码
        lblPassword = new JLabel("密码:");
        lblPassword.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        txtPassword = new JPasswordField(20);
        txtPassword.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 电话
        lblPhone = new JLabel("手机号:");
        lblPhone.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        txtPhone = new JTextField(20);
        txtPhone.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 邮箱
        lblEmail = new JLabel("邮箱:");
        lblEmail.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        txtEmail = new JTextField(20);
        txtEmail.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 提交按钮
        btnSubmit = new JButton("修改");
        btnSubmit.setFont(new Font("微软雅黑", Font.BOLD, 16));
        btnSubmit.setBackground(new Color(0, 123, 255));  // 蓝色按钮背景
        btnSubmit.setForeground(Color.WHITE);  // 白色字体
        btnSubmit.setFocusPainted(false);
        btnSubmit.setPreferredSize(new Dimension(100, 40));
        btnSubmit.setBorder(BorderFactory.createLineBorder(new Color(0, 105, 217), 2));
        btnSubmit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));  // 手型光标

        // 布局添加组件
        gbc.gridx = 0;
        gbc.gridy = 0;
        formPanel.add(lblUserNo, gbc);
        gbc.gridx = 1;
        formPanel.add(txtUserNo, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        formPanel.add(lblName, gbc);
        gbc.gridx = 1;
        formPanel.add(txtName, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        formPanel.add(lblGender, gbc);
        gbc.gridx = 1;
        formPanel.add(cmbGender, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        formPanel.add(lblPassword, gbc);
        gbc.gridx = 1;
        formPanel.add(txtPassword, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        formPanel.add(lblPhone, gbc);
        gbc.gridx = 1;
        formPanel.add(txtPhone, gbc);

        gbc.gridx = 0;
        gbc.gridy = 5;
        formPanel.add(lblEmail, gbc);
        gbc.gridx = 1;
        formPanel.add(txtEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 6;
        formPanel.add(btnSubmit, gbc);

        add(formPanel, BorderLayout.CENTER);

        // 加载用户信息
        try {
            loadUserProfile(Uno);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 提交按钮事件
        btnSubmit.addActionListener(e -> {
            try {
                updateUserProfile(Uno);
                JOptionPane.showMessageDialog(this, "信息更新成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "信息更新失败: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
    }

    private void loadUserProfile(String Uno) throws SQLException {
        String sql = "SELECT Uname, Usex, Upassword, Utel, Uemail FROM user WHERE Uno = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, Uno);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    txtName.setText(rs.getString("Uname"));
                    cmbGender.setSelectedItem(rs.getString("Usex"));
                    txtPassword.setText(rs.getString("Upassword"));
                    txtPhone.setText(rs.getString("Utel"));
                    txtEmail.setText(rs.getString("Uemail"));
                }
            }
        }
    }

    private void updateUserProfile(String Uno) throws SQLException {
        String sql = """
                UPDATE user
                SET Uname = ?, Usex = ?, Upassword = ?, Utel = ?, Uemail = ?
                WHERE Uno = ?
                """;
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, txtName.getText().trim());
            ps.setString(2, (String) cmbGender.getSelectedItem());
            ps.setString(3, new String(txtPassword.getPassword()).trim());
            ps.setString(4, txtPhone.getText().trim());
            ps.setString(5, txtEmail.getText().trim());
            ps.setString(6, Uno);

            ps.executeUpdate();
        }
    }

    public static void main(String[] args) throws SQLException {
        JFrame frame = new JFrame("个人信息管理");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);  // 窗口居中
        frame.setContentPane(new UserProfileUI("U002")); // 示例用户编号
        frame.setVisible(true);
    }
}
